package com.estate.web.controller.eastate;

import java.util.ArrayList;
import java.util.List;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.estate.common.utils.redis.RedisUtil;
import com.estate.system.domain.Residential;
import com.estate.system.domain.ResidentialDetail;
import com.estate.system.service.IResidentialDetailService;
import com.estate.system.service.IResidentialService;
import com.estate.system.service.ISysAdvantageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.estate.common.annotation.Log;
import com.estate.common.enums.BusinessType;


import com.estate.common.core.controller.BaseController;
import com.estate.common.core.domain.AjaxResult;
import com.estate.common.utils.poi.ExcelUtil;
import com.estate.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Controller
 *
 * @author estate
 * @date 2022-04-08
 */
@Controller
@CrossOrigin
@RequestMapping("/system/residential")

public class ResidentialController extends BaseController
{
    private String prefix = "system/residential";

    @Autowired
    private IResidentialService residentialService;
    @Autowired
    private IResidentialDetailService residentialDetailService;
    @Autowired
    private ISysAdvantageService advantageService;
    @Resource
    RedisUtil redisUtil;

    @RequiresPermissions("system:residential:view")
    @GetMapping()
    public String residential()
    {
        return prefix + "/residential";
    }

    /**
     *  【跳转到advantage页面】
     * **/
    @GetMapping("/advantage")
    public String toPost()
    {
        return "system/advantage/advantage";
    }

    /**
     * 查询【请填写功能名称】列表
     */

//    @PostMapping("/list")
//    @ResponseBody
//    public TableDataInfo list(Residential residential)
//    {
//
//        List<Residential> list = residentialService.selectAllResidentialList();
//        return getDataTable(list);
//    }


    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo listByPageHelper(Residential residential, int pageSize, int pageNum)
    {
        //如果这几个其中有一个都不为空则调用数据库的语句去查询
        System.out.println(residential);
        if (!residential.getTitle().equals("") || !residential.getTypehouseArea().equals("") || residential.getPrice() !=null
                || !residential.getAdvantages().equals("") || residential.getIfNeedShow()!=null
        ){
            startPage();
            List<Residential> list = residentialService.selectResidentialListByMapper(residential);
            System.out.println("1");
            System.out.println(list);
            return getDataTable(list);


        }else {
            //混合pagehelper和redis的分页
            //但是不能像mysql一样做条件查询
            //使用redis分页,需要传参数过去
            System.out.println("2");
            PageInfo<Residential> residentialPageInfo = residentialService.selectAllResidentialList(pageSize, pageNum);
            residentialPageInfo.setPageSize(10);
            residentialPageInfo.setPageNum(1);
            System.out.println("list:"+residentialPageInfo);
            List<Residential> list = residentialPageInfo.getList();
            TableDataInfo dataTable = getDataTable(list);
            dataTable.setTotal(residentialPageInfo.getTotal());
            System.out.println("residentialPageInfo.getTotal()"+residentialPageInfo.getTotal());


            return dataTable;
        }

    }
    /**
     * 直接分页
     * **/
    @PostMapping("/list/{pageSize}/{pageNum}")
    @ResponseBody
    public PageInfo<Residential> listByPageHelper(@PathVariable int pageSize,@PathVariable int pageNum)
    {
        //混合pagehelper和redis的分页
        //但是不能像mysql一样做条件查询
        //使用redis分页,需要传参数过去
        System.out.println("2");
        PageInfo<Residential> residentialPageInfo = residentialService.selectAllResidentialList(pageSize, pageNum);
        residentialPageInfo.setPageSize(10);
        residentialPageInfo.setPageNum(1);

        return residentialPageInfo;


    }
    /**
     * 通过title字段模糊查询,并且分页
     *
     * **/
    @PostMapping("/list/title/{pageSize}/{pageNum}")
    @ResponseBody
    public PageInfo<Residential> listByTitlePageHelper(@PathVariable int pageSize,@PathVariable int pageNum,String title)
    {

        Residential residential = new Residential();
        residential.setTitle(title);
        PageHelper.startPage(pageNum,pageSize);
        List<Residential> list = residentialService.selectByTitleList(residential);
        PageInfo<Residential> pageInfo = new PageInfo<Residential>(list);

        return pageInfo;
    }



    /**
     * 得到上传的图片地址并返回标签,1张图片
     */

    @PostMapping("/selectResidentialImg")
    @ResponseBody
    public String afterUploadFileUpdateData(int residentialId)
    {
        Residential residential = residentialService.selectResidentialById((long) residentialId);
        System.out.println("residential119"+residential);
        return residential.getImgAddress();
    }
    /**
     * 得到advantage的值,便于回显优点标签数据
     */
    @PostMapping("/getAdvantageResidential")
    @ResponseBody
    public String getAdvantageResidential(int residentialId)
    {
        Residential residential = residentialService.selectResidentialById((long) residentialId);

        return residential.getAdvantages();
    }


    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:residential:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Residential residential)
    {
        List<Residential> list = residentialService.selectResidentialList(residential);
        ExcelUtil<Residential> util = new ExcelUtil<Residential>(Residential.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("advantages", advantageService.selectAdvantageAll());
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:residential:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Residential residential)
    {
        int res=residentialService.insertResidential(residential);
        if (res ==1){
            return toAjax(true).put("id",residential.getId());
        }
        return toAjax(false).put("id",0);
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:residential:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Residential residential = residentialService.selectResidentialById(id);
        ResidentialDetail residentialDetail = residentialDetailService.selectResidentialDetailById(id);
        mmap.put("residential", residential);
        mmap.put("residentialDetail", residentialDetail);
        mmap.put("id",id);
        mmap.put("advantages", advantageService.selectAdvantageAll());
        return prefix + "/edit";
    }
    //    @RequiresPermissions("system:residential:edit")
    @GetMapping("/edit_detail/{id}")
    public String edit_detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        //根据id搜索,detail表的数据,不存在则返回null
        Residential residential = residentialService.selectResidentialById(id);
        mmap.put("residential", residential);
        ResidentialDetail residentialDetail = residentialDetailService.selectResidentialDetailById(id);
        mmap.put("residentialDetail", residentialDetail);
        mmap.put("advantages", advantageService.selectAdvantageAll());
        return prefix + "/edit_detail";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:residential:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Residential residential,ModelMap mmap)
    {



        return toAjax(residentialService.updateResidential(residential));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:residential:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(residentialService.deleteResidentialByIds(ids));
    }
}
